package ru.pb.game.network.engine.client.packets.receivable;

import ru.pb.game.handlers.AdminCommandHandler;
import ru.pb.game.handlers.ChatHandler;
import ru.pb.game.handlers.interfaces.IAdminCommandHandler;
import ru.pb.game.handlers.interfaces.IChatHandler;
import ru.pb.game.network.engine.client.packets.FromClientToGameServerPacket;
import ru.pb.game.objects.model.chat.Chat;
import ru.pb.game.objects.model.players.Player;

public class PROTOCOL_LOBBY_CHATTING_REQ extends FromClientToGameServerPacket
{
	private int _chat_type, _string_len;
	private String _text;

	public PROTOCOL_LOBBY_CHATTING_REQ()
	{}

	@Override
	public void readImpl()
	{
		readH();
		_chat_type = readH();
		_string_len = readH();
		_text = readS(_string_len);
	}

	@Override
	public void runImpl()
	{
		Player p = getPlayer();
		if(p != null)
		{
			if(_text.startsWith("./"))
			{
				String cmd = "admin_" + _text.substring(2).trim();
				IAdminCommandHandler vch = AdminCommandHandler.getInstance().getAdminCommandHandler(cmd);
				if(vch != null)
				{
					AdminCommandHandler.getInstance().useAdminCommandHandler(p, cmd);
					return;
				}
			}
			IChatHandler handler = ChatHandler.getInstance().get(_chat_type);
			if(handler != null)
				handler.say(p, new Chat(_chat_type, _string_len, _text));
		}
	}
}